home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / EMULATOR / GBDK_BIN / examples / c / struct < prev    next >
Text File  |  1996-04-15  |  2KB  |  70 lines

  1. #include<stdio.h>
  2.  
  3. typedef struct point { int x,y; } point;
  4. typedef struct rect { point pt1, pt2; } rect;
  5.  
  6. point addpoint(point p1, point p2) {    /* add two points */
  7.     p1.x += p2.x;
  8.     p1.y += p2.y;
  9.     return p1;
  10. }
  11.  
  12. #define min(a, b) ((a) < (b) ? (a) : (b))
  13. #define max(a, b) ((a) > (b) ? (a) : (b))
  14.  
  15. rect canonrect(rect r) {        /* canonicalize rectangle coordinates */
  16.     rect temp;
  17.  
  18.     temp.pt1.x = min(r.pt1.x, r.pt2.x);
  19.     temp.pt1.y = min(r.pt1.y, r.pt2.y);
  20.     temp.pt2.x = max(r.pt1.x, r.pt2.x);
  21.     temp.pt2.y = max(r.pt1.y, r.pt2.y);
  22.     return temp;
  23. }
  24.  
  25. point makepoint(int x, int y) {        /* make a point from x and y components */
  26.     point p;
  27.  
  28.     p.x = x;
  29.     p.y = y;
  30.     return p;
  31. }
  32.  
  33. rect makerect(point p1, point p2) {    /* make a rectangle from two points */
  34.     rect r;
  35.  
  36.     r.pt1 = p1;
  37.     r.pt2 = p2;
  38.     return canonrect(r);
  39. }
  40.  
  41. int ptinrect(point p, rect r) {        /* is p in r? */
  42.     return p.x >= r.pt1.x && p.x < r.pt2.x
  43.         && p.y >= r.pt1.y && p.y < r.pt2.y;
  44. }
  45.  
  46. struct odd {char a[3]; } y = {'a', 'b', 0};
  47.  
  48. odd(struct odd y) {
  49.     struct odd x = y;
  50.     printf("%s\n", x.a);
  51. }
  52.  
  53. main() {
  54.     int i;
  55.     point x, origin = { 0, 0 }, maxpt = { 320, 320 };
  56.     point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 };
  57.     rect screen = makerect(addpoint(maxpt, makepoint(-10, -10)),
  58.         addpoint(origin, makepoint(10, 10)));
  59.  
  60.     for (i = 0; i < sizeof pts/sizeof pts[0]; i++) {
  61.         printf("(%d,%d) is ", pts[i].x,
  62.             (x = makepoint(pts[i].x, pts[i].y)).y);
  63.         if (ptinrect(x, screen) == 0)
  64.             printf("not ");
  65.         printf("within [%d,%d; %d,%d]\n", screen.pt1.x, screen.pt1.y,
  66.             screen.pt2.x, screen.pt2.y);
  67.     }
  68.     odd(y);
  69. }
  70.